/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ${USER}
 * Date: ${YEAR}-${MONTH}-${DAY}
 * Time: ${TIME}
 */
public class BinarySearchTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }
    public TreeNode root; //祖宗节点

    public TreeNode search(int key){
        if(root == null){
            return null;
        }
        TreeNode cur = root;
        while(cur != null){
            if(cur.val < key){
                cur = cur.right;
            }else if(cur.val > key){
                cur = cur.left;
            }else{
                return cur;
            }
        }
        return null;
    }
    public boolean insert(int val){
        if(root == null){   //根节点为空，直接插入根节点
            root = new TreeNode(val);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else{
                return false; //二叉搜索树不允许有重复节点，直接返回false
            }
        }
        if(parent.left == cur){
            parent.left = new TreeNode(val);
        }else{
            parent.right = new TreeNode(val);
        }
        return true;
    }

    public void remove(int val){    //重点
        if(root == null){
            return;
        }
        //先找到要删除的节点
        TreeNode cur = root;
        TreeNode parent = null; //标记删除节点的父亲节点，方便父亲节点的子节点被删除后，让父亲节点管理孙子节点
        while(cur != null){
            if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }else{
                //找到了进行删除
                removeNode(parent, cur);
                return;
            }
        }

    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null){   //子节点的左边为空
            if(root == cur) {   //如果删除的节点是祖宗节点，祖宗节点的父节点为空，那么子节点的右节点就作为新的祖宗节点
                root = cur.right;
            }
            else if(parent.left == cur){
                parent.left = cur.right;
            }else{
                parent.right = cur.right;
            }
        }else if(cur.right == null){    //子节点的右边为空
            if(root == cur){
                root = cur.left;
            }else if(parent.left == cur){
                parent.left = cur.left;
            }else{
                parent.right = cur.left;
            }
        }else{  //子节点的左右都不为空
            TreeNode minRight = cur.right;    //右子树的最左子树
            TreeNode minRightParent = cur;  //标记最左子树的父节点，方便删除最左子树后，管理最左子树的右节点, 这里不能把他设置null,防止cur.right就是最左节点，对下面minRightparent解引用空指针
            while(minRight.left != null){   //找到最左子树
                minRightParent = minRight;
                minRight = minRight.left;
            }
            cur.val = minRight.val;
            if(minRightParent.right == minRight){   //特殊的情况，cur.right就是最左节点，所以minRight是父亲的右子树
                minRightParent.right = minRight.right;  //指向right是因为我们删除的是最左节点，不可能还有左节点
            }else{
                minRightParent.left = minRight.right; //正常的情况，minRight一定是父亲的最左节点
            }
        }
    }
}